package com.cqcet.wenchuang.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cqcet.wenchuang.dto.GoodsDto;
import com.cqcet.wenchuang.entity.Goods;
import com.cqcet.wenchuang.mapper.GoodsMapper;
import com.cqcet.wenchuang.service.GoodsService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Objects;

@Service
@Transactional
public class GoodsServiceImpl extends ServiceImpl<GoodsMapper, Goods> implements GoodsService {

    /**
     * 根据条件查询
     *
     * @param goodsDto
     * @return
     */
    @Override
    public List<Goods> goodsList(GoodsDto goodsDto) {

        LambdaQueryWrapper<Goods> queryWrapper = new LambdaQueryWrapper<>();
        //分类id不为null则添加分类查询
        queryWrapper.eq(!Objects.isNull(goodsDto.getCategoryId()), Goods::getCategoryId, goodsDto.getCategoryId());
        //价格小于等于
        queryWrapper.le(!Objects.isNull(goodsDto.getLessThen()), Goods::getPrice, goodsDto.getLessThen());
        //价格大于等于
        queryWrapper.ge(!Objects.isNull(goodsDto.getMoreThen()), Goods::getPrice, goodsDto.getMoreThen());
        //根据名字模糊搜索
        queryWrapper.like(!Objects.isNull(goodsDto.getName()), Goods::getName, goodsDto.getName());
        //商品状态
        queryWrapper.eq(!Objects.isNull(goodsDto.getStatus()), Goods::getStatus, goodsDto.getStatus());

        return this.list(queryWrapper);
    }
}
